<template>
    <el-table :data="PermissionArr" style="width: 100%; margin-bottom: 20px" row-key="id" border>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="权限值" prop="code"></el-table-column>
        <el-table-column label="修改时间" prop="updateTime"></el-table-column>
        <el-table-column label="操作">
            <template #default="scope">
                <el-button type="primary" @click="addPermission(scope.row)" size="small"
                    :disabled="scope.row.level === 4">{{
                        scope.row.level === 3 ? '添加功能' : '添加菜单' }}</el-button>
                <el-button type="primary" @click="updatePermission(scope.row)" size="small"
                    :disabled="scope.row.level === 1">编辑</el-button>
                <el-popconfirm :title="`你确定要删除${scope.row.name}?`" width="260px" @confirm="removeMenu(scope.row.id)">
                    <template #reference>
                        <el-button :disabled="scope.row.level == 1 ? true : false" size="small" type="primary">
                            删除
                        </el-button>
                    </template>
                </el-popconfirm>
            </template>
        </el-table-column>
    </el-table>
    <!-- 对话框组件:添加或者更新已有的菜单的数据结构 -->
    <el-dialog v-model="dialogVisible" :title="menuData.id ? '更新菜单' : '添加菜单'">
        <!-- 表单组件:收集新增与已有的菜单的数据 -->
        <el-form>
            <el-form-item label="名称">
                <el-input v-model="menuData.name" placeholder="请你输入菜单名称"></el-input>
            </el-form-item>
            <el-form-item label="权限">
                <el-input v-model="menuData.code" placeholder="请你输入权限数值"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="save">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reqAddOrUpdateMenu, reqAllPermission, reqRemoveMenu } from '@/api/acl/menu';
import type { MenuParams, Permission, PermissionList, PermissionResponseData } from '@/api/acl/menu/type';
import { ElMessage } from 'element-plus';
import { onMounted, reactive, ref } from 'vue';

//存储菜单的数据
let PermissionArr = ref<PermissionList>([])
//控制对话框的显示与隐藏
let dialogVisible = ref<boolean>(false)
//携带的参数
let menuData = reactive<MenuParams>({
    code: '',
    level: 0,
    name: '',
    pid: 0,
})

onMounted(() => {
    //获取菜单数据的方法
    getHasPermission()

})
const getHasPermission = async () => {
    let result: PermissionResponseData = await reqAllPermission()
    if (result.code === 200) {
        PermissionArr.value = result.data
    }
}
const addPermission = (row: Permission) => {
    Object.assign(menuData, {
        id: 0,
        code: '',
        level: 0,
        name: '',
        pid: 0,
    })
    dialogVisible.value = true
    menuData.level = row.level + 1
    menuData.pid = row.id as number

}
const updatePermission = (row: Permission) => {
    dialogVisible.value = true
    Object.assign(menuData, row)
}

const save = () => {
    reqAddOrUpdateMenu(menuData).then(res => {
        if (res.code === 200) {
            dialogVisible.value = false
            ElMessage.success('保存成功')
            getHasPermission()
        } else {
            ElMessage.error(res.message)
        }
    })
}

const removeMenu = (id: number) => {
    reqRemoveMenu(id).then(res => {
        if (res.code === 200) {
            ElMessage.success('删除成功')
            getHasPermission()
        } else {
            ElMessage.error(res.message)
        }
    })
}
</script>

<style scoped></style>